 /*************************************************************************************************/
  *                                                                                               *
  *   Do file to replicate tables and appendices in                                               *
  *   “The Political Economy of Bilaterall Bailouts”                                              *
  *   Forthcoming in International Organization                                                   *
  *   Authors: Christina Schneider and Jennifer Tobin*/                                           *
  *   Date: July 6, 2019                                                                          *
  *                                                                                               *
 /*************************************************************************************************/
clear
clear matrix
 
 /*Change working directory*/
cd  "C:\Users\"

/*open main analytic dataset*/
use "schneider_tobin_bailouts_final.dta", clear

set more off



/*1st stage regression for endogeneity of IMF loan: create residuals*/
reg total_imf_loans_cd_ln imf_instrument trade_exposure_ln alliance  democracy preference_sim  election_timing veto_players  d_gdp_grow   d_unemployment d_gdp_pc c_gdp_pc c_cur_acct_gdp ldist   spatialweight_imf_g7 if g7==1, robust cluster(iso_d)
predict total_imf_loans_hat_resid, residuals

/*****************************Appendix B: Summary Statistics************************************************/
sum bilateral_bailout trade_exposure financial_exposure  alliance  democracy preference_sim  election_timing veto_players d_gdp_grow   d_unemployment d_gdp_pc c_gdp_pc c_cur_acct_gdp ldist total_imf_loans_cd_ln if g7==1



/*Create 5 imputed datasets*/
mi set mlong
mi register imputed alliance bailoutfatigue	banking_crisis	bilateral_bailout	c_cur_acct_gdp	c_gdp_pc c_shrtdebt_reserves c_debts_exports c_debt_gdp	currency_crisis_rr	d_gdp_grow	d_gdp_pc	d_unemployment	democracy	dom_sov_debt_crisis	election_timing	financial_exposure_ln	imf_instrument	ldist	ldist_inverse	preference_sim	shared_region	stock_market_crash	total_imf_loans_cd_ln	total_imf_loans_hat_resid trade_exposure_ln	veto_players    
mi impute chained (logit) alliance banking_crisis	bilateral_bailout currency_crisis_rr democracy dom_sov_debt_crisis election_timing shared_region	stock_market_crash (regress) bailoutfatigue  c_cur_acct_gdp	c_gdp_pc c_shrtdebt_reserves c_debts_exports c_debt_gdp	d_gdp_grow	d_gdp_pc	d_unemployment 	financial_exposure_ln	imf_instrument	ldist	ldist_inverse preference_sim		total_imf_loans_cd_ln	total_imf_loans_hat_resid trade_exposure_ln	veto_players = year panel_id_num, add(5) rseed (54321)  


/*to create 10 imputed datasets for robustness check Appendix C.1, Model 2 use:*/
/*mi impute chained (logit) alliance banking_crisis	bilateral_bailout currency_crisis_rr democracy dom_sov_debt_crisis election_timing shared_region	stock_market_crash (regress) bailoutfatigue  c_cur_acct_gdp	c_gdp_pc c_shrtdebt_reserves c_debts_exports c_debt_gdp	d_gdp_grow	d_gdp_pc	d_unemployment 	financial_exposure_ln	imf_instrument	ldist	ldist_inverse preference_sim		total_imf_loans_cd_ln	total_imf_loans_hat_resid trade_exposure_ln	veto_players = year panel_id_num, add(10) rseed (54321)   */


/*create combined proxy for economic exposure*/
factor  trade_exposure_ln  financial_exposure_ln, pcf
rotate
predict econ_exp_factor



/*create spatial weights for main analysis and robustness checks*/

spspc bilateral_bailout, source(donor_country) target (crisis_country) weightvar(ldist_inverse) form(source) link(ik)
 rename SE_var_sp_source_rowst spatialweight_dist_ik

spspc bilateral_bailout, source(donor_country) target (crisis_country) weightvar(trade_exposure_ln) form(source) link(ik)
rename SE_var_sp_source_rowst spatialweight_trade_ik
 
spspc bilateral_bailout, source(donor_country) target (crisis_country) weightvar(trade_exposure_ln) form(source) link(kj)
 rename SE_var_sp_source_rowst spatialweight_trade_jk

spspc bilateral_bailout, source(donor_country) target (crisis_country) weightvar(ldist_inverse) form(source) link(kj)
rename SE_var_sp_source_rowst spatialweight_dist_jk




/*standardize each continuous variable to a mean of 0 and a standard deviation of 1/2*/

egen trade_exposure_ln_std=std(trade_exposure_ln)
gen trade_exposure_ln_hstd=.5*trade_exposure_ln_std
egen financial_exposure_ln_std=std(financial_exposure_ln)
gen financial_exposure_ln_hstd=.5*financial_exposure_ln_std
egen econ_exp_factor_std=std( econ_exp_factor)
gen  econ_exp_factor_hstd=.5*econ_exp_factor_std
egen preference_sim_std=std( preference_sim )
gen preference_sim_hstd =.5*preference_sim_std
egen veto_players_std=std(veto_players )
gen veto_players_hstd =.5*  veto_players_std
egen d_gdp_grow_std=std( d_gdp_grow  )
gen  d_gdp_grow_hstd =.5*d_gdp_grow_std
egen d_unemployment_std=std(d_unemployment )
gen  d_unemployment_hstd =.5*d_unemployment_std
egen d_gdp_pc_std=std(d_gdp_pc )
gen   d_gdp_pc_hstd=.5*d_gdp_pc_std
egen c_gdp_pc_std=std(c_gdp_pc )
gen  c_gdp_pc_hstd =.5*c_gdp_pc_std
egen c_cur_acct_gdp_std=std(c_cur_acct_gdp )
gen  c_cur_acct_gdp_hstd =.5*c_cur_acct_gdp_std
egen ldist_std=std(ldist )
gen   ldist_hstd=.5*ldist_std
egen total_imf_loans_cd_ln_std=std(total_imf_loans_cd_ln )
gen  total_imf_loans_cd_ln_hstd =.5*total_imf_loans_cd_ln_std
egen total_imf_loans_hat_resid_std=std( total_imf_loans_hat_resid  )
gen  total_imf_loans_hat_resid_hstd =.5*total_imf_loans_hat_resid_std
egen spatialweight_dist_ik_std=std(spatialweight_dist_ik )
gen   spatialweight_dist_ik_hstd=.5*spatialweight_dist_ik_std
egen spatialweight_trade_ik_std=std(spatialweight_trade_ik )
gen   spatialweight_trade_ik_hstd=.5*spatialweight_trade_ik_std
egen spatialweight_trade_jk_std=std(spatialweight_trade_jk )
gen   spatialweight_trade_jk_hstd=.5*spatialweight_trade_jk_std
egen spatialweight_dist_jk_std=std(spatialweight_dist_jk )
gen   spatialweight_dist_jk_hstd=.5*spatialweight_dist_jk_std
egen c_shrtdebt_reserves_std=std(c_shrtdebt_reserves )
gen   c_shrtdebt_reserves_hstd=.5*c_shrtdebt_reserves_std
egen c_debts_exports_std=std( c_debts_exports )
gen   c_debts_exports_hstd=.5*c_debts_exports_std
egen c_debt_gdp_std=std(c_debt_gdp )
gen  c_debt_gdp_hstd =.5*c_debt_gdp_std



/*main regressions*/

/*Table 2*/
eststo clear

/**********************************Table 1****************************/
/*main model with trade exposure*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)

/*main model with financial exposure*/
eststo: mi estimate, post: logit  bilateral_bailout financial_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)

/*main model with pca proxy for econ*/
eststo: mi estimate, post: logit  bilateral_bailout econ_exp_factor_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)


/*produce table*/
esttab using "table1.csv", star(* 0.10 ** 0.05 *** .01) scalars(F_mi p) label se replace


/**********************************Appendix C.1 changes to specification****************************/
eststo clear


/*C.1.*/
/*listwise deletion: */
eststo:  logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1 & _mi_m==0, robust cluster(donor_country)

/* 10 imputations: restart do-file changing imputation code to one listed above */
/*eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)*/

/*time trend*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd  year if g7==1, robust cluster(donor_country)

/*produce table*/
esttab using "appendix_specification.csv", star(* 0.10 ** 0.05 *** .01) scalars(F_mi p) label se replace

/**********************************Appendix C.2 changes to spatial weights****************************/


eststo clear

/*C.2.*/

/*main model without spatial weight*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd     if g7==1, robust cluster(donor_country)

/*spatial weights */
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_trade_ik  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_jk  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1, robust cluster(donor_country)
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_trade_jk   d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd  if g7==1, robust cluster(donor_country)

/*produce table*/
esttab using "appendix_spatialweight.csv", star(* 0.10 ** 0.05 *** .01) scalars(F_mi p) label se replace



/**********************************Appendix C.3 additional controls****************************/

eststo clear

/*C.3.*/

/*bailout fatigue*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   bailoutfatigue if g7==1, robust cluster(donor_country)

/*type of crisis*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd  banking_crisis currency_crisis_rr dom_sov_debt_crisis stock_market_crash  if g7==1, robust cluster(donor_country)

/*same region*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd  shared_region  if g7==1, robust cluster(donor_country)

/*exclude Greece and Ireland*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   if g7==1 & crisis_country~="Greece" & crisis_country~="Ireland", robust cluster(donor_country)

/*produce table*/
esttab using "appendix_controls.csv", star(* 0.10 ** 0.05 *** .01) scalars(F_mi p) label se replace



/**********************************Appendix C.4 macroeconomic controls***********************************/
eststo clear

/*C.4.*/


/*debt to reserves*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   c_shrtdebt_reserves_hstd if g7==1, robust cluster(donor_country)
/*debt to exports*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   c_debts_exports_hstd  if g7==1, robust cluster(donor_country)
/*debt to gdp*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   c_debt_gdp_hstd  if g7==1, robust cluster(donor_country)
/*combined*/
eststo: mi estimate, post: logit  bilateral_bailout trade_exposure_ln_hstd  alliance  democracy preference_sim_hstd  election_timing veto_players_hstd total_imf_loans_cd_ln_hstd total_imf_loans_hat_resid_hstd   spatialweight_dist_ik_hstd  d_gdp_grow_hstd   d_unemployment_hstd d_gdp_pc_hstd c_gdp_pc_hstd c_cur_acct_gdp_hstd ldist_hstd   c_shrtdebt_reserves_hstd c_debts_exports_hstd c_debt_gdp_hstd  if g7==1, robust cluster(donor_country)


/*produce table*/
esttab using "appendix_macrocontrols.csv", star(* 0.10 ** 0.05 *** .01) scalars(F_mi p) label se replace


